package org.hybridlabs.source.formatter;

/**
 * Copyright 2008 hybrid labs
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;

import org.hybridlabs.file.FileHelper;
import org.hybridlabs.source.beautifier.BeautifierWithConvention;
import org.hybridlabs.source.beautifier.CharacterSequence;
import org.hybridlabs.source.beautifier.JavaImportBeautifierImpl;

/**
 * Simple M2T Xpand beautifier implementation based on Jalopy and the antlr JavaNode.
 * The implementation substitutes fully qualified class names (as long as there
 * is no conflict) with the short name and the according import statement.
 *
 * @author Karsten Klein, hybrid labs
 * @author Karsten Thoms, itemis
 *
 */
public class JavaImportBeautifier implements org.eclipse.xpand2.output.PostProcessor,
		BeautifierWithConvention {

	/**
	 * The beautifier implementation doing the real job.
	 */
	private JavaImportBeautifierImpl beautifier = new JavaImportBeautifierImpl();

	public static void main(String[] args) throws IOException {
		File sourceFile = new File(args[0]);
		File targetFile = (args.length > 0) ? new File(args[1]) : null;

		JavaImportBeautifier beautifier = new JavaImportBeautifier();
		beautifier.setFormat(true);
		beautifier.setOrganizeImports(true);
		beautifier.beautify(sourceFile, targetFile);
	}

	public String getConventionFilePath() {
		return beautifier.getConventionFilePath();
	}

	/**
	 * {@inheritDoc}
	 */
	public void setConventionFilePath(String conventionFilePath) {
		beautifier.setConventionFilePath(conventionFilePath);
	}

	public boolean isFormat() {
		return beautifier.isFormat();
	}

	public void setFormat(boolean isFormat) {
		beautifier.setFormat(isFormat);
	}

	public boolean isOrganizeImports() {
		return beautifier.isOrganizeImports();
	}

	public void setOrganizeImports(boolean isOrganizeImports) {
		beautifier.setOrganizeImports(isOrganizeImports);
	}

    public boolean isStrict() {
        return beautifier.isStrict();
    }

    public void setStrict(boolean isStrict) {
    	beautifier.setStrict(isStrict);
    }

	public void beautify(CharacterSequence sb) {
		beautifier.beautify(sb);
	}

	/**
	 * Beautifies the source file and writes the result to the traget file.
	 * Source and target may be identical.
	 *
	 * @param sourceFile
	 * @param targetFile
	 * @throws FileNotFoundException
	 * @throws IOException
	 */
	public void beautify(File sourceFile, File targetFile)
			throws FileNotFoundException, IOException {
		StringBuffer buffer = FileHelper.loadStringBuffer(sourceFile);

		CharacterSequence sequence = new CharacterSequence(buffer);
		beautify(sequence);

		String targetPath = sourceFile.getAbsolutePath();
		if (targetFile != null) {
			targetPath = targetFile.getAbsolutePath();
		}

		FileWriter writer = new FileWriter(new File(targetPath));
		writer.write(sequence.toString());
		writer.close();
	}

	/**
	 * Implementation of the M2T Xpand PostProcessor-defined method.
	 */
	public void beforeWriteAndClose(org.eclipse.xpand2.output.FileHandle info) {
		if (info.getTargetFile() != null
				&& info.getTargetFile().getPath().endsWith(".java")) {

			CharSequence charSequence = null;

			charSequence = info.getBuffer();

			CharacterSequence sequence = new CharacterSequence(charSequence);
			beautify(sequence);
			if (sequence.getStringBuffer() != null
					&& sequence.getStringBuffer().length() > 0) {
				info.setBuffer(sequence);
			}
		}
	}

	/**
	 * Implementation of the oAW PostProcessor-defined method.
	 */
	public void afterClose(org.eclipse.xpand2.output.FileHandle impl) {
		// NOTE nothing to do here
	}

}
